<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>导航守卫</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.4.3/vue-router.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.20.0/axios.js"></script>
  </head>
  <body>
    <div id="app">
      <router-link to="/home">首页</router-link>
      <router-link to="/news">新闻</router-link>
      <router-link to="/music">音乐</router-link>
      <router-link to="/login">登录</router-link>
      <router-view></router-view>
    </div>
    <template id="login">
      <div>
        <input type="text" />
        <input type="text" />
        <button type="button" @click="login">登录</button>
      </div>
    </template>

    <script>
      //up；园姐码上说 [东软睿道] https://www.bilibili.com/video/BV1F54y1q7VD?from=search&seid=2554064219431268586
      //1.定义路由模板组件
      const home = { template: "<div>首页</div>" };
      const news = { template: "<div>新闻</div>" };
      const music = { template: "<div>音乐</div>" };
      const login = {
        template: "#login",
        data() {
          return {
            username: "",
            password: "",
          };
        },
        methods: {
          login() {
            //发送ajax请求,请求成功了,返回用户信息[客户端请求服务器,服务器返回token信息保存]
            let token = {
              username: this.username,
              password: this.password,
              priviledge: "admin",
            };
            //往客户端存放cookie:localstorage[本地磁盘],sessionStorage[网页窗口不关,就有,关了就没有]
            localStorage.setItem("token", token); //登录成功后,服务器返回一个token,然后把token放在客户端,下次在登陆会自动携带
          },
        },
      };
      //2.定义路由映射关系
      const routes = [
        { path: "/", name: "default", redirect: "/home" },
        { path: "/home", name: "home", component: home },
        { path: "/news", name: "news", component: news },
        { path: "/music", name: "music", component: music },
        { path: "/login", name: "login", component: login },
      ];
      //3.定义路由
      const router = new VueRouter({
        routes,
      });
      //全局路由守卫
      router.beforeEach((to, from, next) => {
        let islogin = false;
        let token = localStorage.getItem("token");
        if (token != null) {
          islogin = true;
        }
        //定义需要登录访问的路由列表
        console.log(to.name);
        let arr = ["news", "music"];
        if (arr.indexOf(to.name) >= 0) {
          if (!islogin) {
            //当上来没登陆时,我们让他路由转向登录
            router.push("/login");
            location.reload(); //解决 Repeated navigation 重复的导航问题
          }
        }
        //如果已经登录过了直接进home
        if (to.name == "login") {
          if (islogin) {
            router.push("/home");
            location.reload();
          }
        }
        next();
      });

      let vm = new Vue({
        el: "#app",
        data() {
          return {};
        },
        router,
      });
    </script>
  </body>
</html>
